home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-12-11 | 10.5 KB | 405 lines | [TEXT/ALFA] |
- ## -*-Tcl-*- (install)
- # ###################################################################
- # Vince's Additions - an extension package for Alpha
- #
- # FILE: "modeSearchPaths.tcl"
- # created: 3/12/96 {6:35:25 pm}
- # last update: 11/12/97 {9:35:57 am}
- # Author: Vince Darley
- # E-mail: <darley@fas.harvard.edu>
- # mail: Division of Applied Sciences, Harvard University
- # Oxford Street, Cambridge MA 02138, USA
- # www: <http://www.fas.harvard.edu/~darley/>
- #
- # ###################################################################
- ##
-
- alpha::extension modeSearchPaths 1.2 {
- menu::insert mode items 6 \
- "viewSearchPath" "appendSearchPaths…" "removeSearchPaths…" "(-"
- newPref binding openSelection "<O<B/H" searchPaths
- newPref binding sourceHeaderToggle "<O/f" searchPaths
- menu::insert winUtils items end \
- "[menu::bind searchPathsmodeVars(sourceHeaderToggle) -]" \
- "[menu::bind searchPathsmodeVars(openSelection) -]"
- menu::insert global items 5 "searchPathPrefs…"
- # make sure we've loaded the old version of this proc
- catch {file::tryToOpen blah blah blah blah}
-
- # ◊◊◊◊ Try to open the given name or selection ◊◊◊◊ #
- proc file::tryToOpen {{fname ""}} {
- if {$fname == ""} {set fname [getSelect]}
- if ![catch {file::_tryToOpen $fname ""}] {
- return
- }
- global headerSuffices sourceSuffices
- if { [file extension ${fname}] == "" } {
- if ![catch {file::_tryToOpen $fname $headerSuffices}] {
- return
- }
- if ![catch {file::_tryToOpen $fname $sourceSuffices}] {
- return
- }
- }
- if {[askyesno "'$fname' can not be found, do you wish to add an include path?"]} {
- mode::appendSearchPath [string trimright [get_directory] ":"]
- mode::modifySearchPath
- return [file::tryToOpen $fname]
- }
- error "Couldn't find anything"
- }
- } uninstall this-file maintainer {
- "Vince Darley" darley@fas.harvard.edu <http://www.fas.harvard.edu/~darley/>
- } help {
- Over-rides the default 'Option title-bar clicking routines',
- allowing them to search more widely, and provides general
- procedures to find files from mode-specific lists of paths,
- plus handling of a new 'include paths' section of the
- 'config->current mode' menu.
-
- By default 'opt-cmd-H' is bound to "openSelection"
- and 'cmd-F2' is bound to "sourceHeaderToggle"
-
- You can change these items in the dialog, but you need to
- restart to have the changes take effect.
-
- This code can also handle the creation and manipulation of
- an 'search paths' menu. You have to attach the menu to
- a given mode's menu.
- }
-
- proc global::searchPathPrefs {} {
- global mode searchPathsmodeVars
- if {$mode != ""} {
- set searchPathsmodeVars(${mode}ModeSearchPath) [mode::getSearchPath]
- dialog::pkg_options searchPaths
- if {[set searchPathsmodeVars(${mode}ModeSearchPath)] != [mode::getSearchPath]} {
- mode::setSearchPath [set searchPathsmodeVars(${mode}ModeSearchPath)]
- }
- unset searchPathsmodeVars(${mode}ModeSearchPath)
- mode::modifySearchPath
- } else {
- dialog::pkg_options searchPaths
- }
- }
-
- # ◊◊◊◊ Include paths ◊◊◊◊ #
-
- #################################################################
- # #
- # Only _ever_ access include paths through these 4 functions. #
- # #
- #################################################################
-
- proc mode::getSearchPath {} {
- global mode
- global ${mode}SearchPath
- if [info exists ${mode}SearchPath] {
- return [set ${mode}SearchPath]
- } else {
- return ""
- }
- }
-
- proc mode::setSearchPath {path} {
- global mode
- global ${mode}SearchPath
- set ${mode}SearchPath $path
- }
-
- proc mode::removeSearchPath {path} {
- global mode
- global ${mode}SearchPath
- if [info exists ${mode}SearchPath] {
- set res [lsearch -exact [set ${mode}SearchPath] $path]
- if {$res != -1} {
- set ${mode}SearchPath [lreplace [set ${mode}SearchPath] $res $res]
- }
- }
- }
-
- proc mode::appendSearchPath {path} {
- global mode
- global ${mode}SearchPath
- lappend ${mode}SearchPath $path
- }
-
- # Now we have the functions which manipulate include paths and menus
-
- proc mode::modifySearchPath {} {
- global mode modifiedVars
- if ![catch {mode::getSearchPath} include] {
- lappend modifiedVars ${mode}SearchPath
- }
- mode::rebuildSearchPathMenu
- }
-
- proc mode::rebuildSearchPathMenu {{name ""}} {
- global mode
- global ${mode}modeVars ${mode}SearchPathMenu
- if {[info exists ${mode}modeVars(includeMenu)] \
- && [set ${mode}modeVars(includeMenu)]} {
- if {$name == ""} {
- if [info exists ${mode}SearchPathMenu] {
- set name [set ${mode}SearchPathMenu]
- } else {
- set name headers
- }
- } else {
- set ${mode}SearchPathMenu $name
- }
- set paths {}
- foreach p [mode::getSearchPath] {
- lappend paths "[dialog::specialView_file $p]&"
- }
- menu -n $name -p mode::includeProc -m [concat {
- "Open"
- "Add Folder…"
- "Remove Folder…"
- "(-"
- } $paths]
- }
- }
-
- proc mode::checkSearchPath {} {
- set bad 0
- if [catch {mode::getSearchPath}] {return [mode::appendSearchPaths]}
- set newInc {}
- foreach p [mode::getSearchPath] {
- if {![file exists $p]} {
- set bad 1
- } else {
- lappend newInc $p
- }
- }
- if $bad {
- mode::setSearchPath $newInc
- mode::modifySearchPath
- }
- return $newInc
- }
-
- # Now the functions the user sees
-
- proc mode::viewSearchPath {} {
- global mode
- listpick -p "Include paths for '$mode' mode:" [mode::getSearchPath]
- }
-
- proc mode::removeSearchPaths {} {
- global mode
- set remove [listpick -p "Remove which items from '$mode' mode's search path:" -l [mode::getSearchPath]]
- foreach r $remove {
- mode::removeSearchPath $r
- }
- mode::modifySearchPath
- }
-
- proc mode::appendSearchPaths {} {
- if [catch {mode::getSearchPath}] {
- mode::setSearchPath {}
- }
- while {![catch {set path [get_directory]}]} {
- set path [string trimright $path : ]
- mode::appendSearchPath $path
- }
- mode::modifySearchPath
- }
-
- proc mode::includeProc {menu item} {
- switch $item {
- "Open" {
- set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
- if {[regexp {["<]([^">]*)[">]} $text dummy fname]} {
- file::tryToOpen $fname
- }
- }
- "Add Folder" {
- mode::appendSearchPath [string trimright [get_directory] ":"]
- mode::modifySearchPath
- }
- "Remove Folder" {
- mode::removeSearchPath [listpick -p "Remove which folder?" [lsort [mode::getSearchPath]]]
- mode::modifySearchPath
- }
- default {
- set i [lsearch -glob [mode::getSearchPath] $item]
- if {$i != -1} {
- if {[askyesno "Shall I reveal this folder in the finder?"] == "yes"} {
- openFolder [lindex [mode::getSearchPath] $i]
- }
- } else {
- alertnote "Sorry, Alpha has a bug which can affect this menu. I can't find that particular folder."
- }
- }
-
- }
- }
-
- proc file::_tryToOpen {fname suffices} {
- # first try basic open
- if ![catch {file::_tryToOpenIn $fname [list [file dir [win::Current]]] $suffices}] {
- return
- }
- # now try in mode include path
- if ![catch {file::_tryToOpenIn $fname [mode::getSearchPath] $suffices}] {
- return
- }
- # now try in common paths
- if ![catch {file::_tryToOpenIn $fname [file::_MakeCommonPaths [file dirname [win::Current]]] $suffices}] {
- return
- }
-
- error "Couldn't find anything"
- }
-
- ##
- # -------------------------------------------------------------------------
- #
- # "fileMakeCommonPaths" --
- #
- # Given a directory make a path of common search possibilities which
- # could pertain to that directory. This is done by matching possible
- # Source/Header directory pairs, and by adding the mode-dependent
- # 'includePath' variable.
- # -------------------------------------------------------------------------
- ##
- proc file::_MakeCommonPaths { thisdir } {
- # allow for some common possibilities of separating
- # source and header files
- set path [list $thisdir]
-
- foreach src { Source src source Src } {
- if { [file tail $thisdir] == $src } {
- foreach dir { Headers headers Header header } {
- lappend path [file dirname $thisdir]:$dir
- }
- break
- }
- }
-
- foreach src { Headers headers Header header } {
- if { [file tail $thisdir] == $src } {
- foreach dir { Source src source Src } {
- lappend path [file dirname $thisdir]:$dir
- }
- break
- }
- }
-
- return $path
- }
-
-
- proc file::_tryToOpenIn { filelist path {suffices ""}} {
- set w [win::Current]
- foreach dir $path {
- foreach ff $filelist {
- if {$suffices != ""} {
- foreach sfx $suffices {
- if {[file exists $dir:$ff$sfx] && "$dir:$ff$sfx" != $w} {
- openFileQuietly $dir:$ff$sfx
- return
- }
- }
- } else {
- if {[file exists $dir:$ff] && "$dir:$ff" != $w} {
- openFileQuietly $dir:$ff
- return
- }
- }
- }
- }
- error "Couldn't find anything"
- }
-
-
- ##
- # ----------------------------------------------------------------------
- #
- # "file::sourceHeaderToggle" --
- #
- # Toggles the front window back and forth between a header/source
- # pair. Requires that "headerSuffices" and "sourceSuffices" be
- # defined for whatever mode it is used in.
- #
- # Side effects:
- # A different window is (perhaps opened) and uppermost
- #
- # ----------------------------------------------------------------------
- ##
- proc file::sourceHeaderToggle {} {
- set ff [win::CurrentTail]
- set fbase [file::baseName $ff]
- set m [modeALike]
- if [file::isSource $ff] {
- global headerSuffices
- file::_tryToOpen ${fbase} $headerSuffices
- } elseif [file::isHeader $ff] {
- global sourceSuffices
- file::_tryToOpen ${fbase} $sourceSuffices
- } else {
- # don't recognise this file
- beep
- message "I don't recognise the file extension. Set your 'sourceSuffices' and 'headerSuffices'"
- return
- }
- }
-
-
- ##
- # ----------------------------------------------------------------------
- #
- # "openSelection" --
- #
- # Opens the header file currently selected, or else if that
- # fails, calls file::sourceHeaderToggle. Each mode can have a variable
- # 'includePath' defined, in which this procedure searches.
- #
- # ----------------------------------------------------------------------
- ##
- proc file::openSelection {} {
- file::tryToOpen
- }
-
- # three simple utility procedures
-
- proc file::isHeader { filename {m ""}} {
- if {$m == ""} {
- global headerSuffices
- set var "headerSuffices"
- } else {
- global dummyProc
- if {[info exists dummyProc($m)]} { $dummyProc($m) }
- global ${m}modeVars
- set var "${m}modeVars(headerSuffices)"
- }
- return [file::_listContains $var [file extension $filename]]
- }
-
- proc file::isSource { filename {m ""}} {
- if {$m == ""} {
- global sourceSuffices
- set var "sourceSuffices"
- } else {
- global dummyProc
- if {[info exists dummyProc($m)]} { $dummyProc($m) }
- global ${m}modeVars
- set var "${m}modeVars(sourceSuffices)"
- }
- return [file::_listContains $var [file extension $filename]]
- }
-
- proc file::_listContains {listvar item} {
- upvar $listvar a
- if [info exists a] {
- return [expr [lsearch -exact $a $item] != -1]
- } else {
- return 0
- }
- }
-
- proc file::baseName { filename } { return [file root [file tail $filename]] }
-
-
-